package com.hejing.cloud.controller;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.hejing.cloud.apis.PayFeignApi;
import com.hejing.cloud.entities.PayDTO;
import com.hejing.cloud.resp.ResultData;
import com.hejing.cloud.resp.ReturnCodeEnum;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * ClassName: OrderController
 * Package: com.hejing.cloud.controller
 * Description:
 *
 * @Author: HeJing
 * @Create: 2024-08-04 8:40
 * @Version: 1.0
 */
@RestController
@Slf4j
public class OrderController {

    @Resource
    private PayFeignApi payFeignApi;

    /**
     * 新增一条流水记录
     * @param payDTO
     * @return
     */
    @PostMapping(value = "/feign/pay/add")
    public ResultData addOrder(@RequestBody PayDTO payDTO) {
        log.info("第一步：模拟本地addOrder新增订单成功(省略sql操作)，第二步：再开启addPay支付微服务远程调用");

        ResultData resultData = payFeignApi.addPay(payDTO);
        return resultData;
    }

    /**
     * 根据id删除记录流水
     * @param id
     * @return
     */
    @DeleteMapping("/feign/pay/del/{id}")
    public ResultData deletePay(@PathVariable("id") Integer id) {

        return payFeignApi.deletePay(id);
    }

    /**
     * 修改记录流水
     * @param payDTO
     * @return
     */
    @PutMapping(value = "/feign/pay/update")
    public ResultData updatePay(@RequestBody PayDTO  payDTO) {
        return payFeignApi.updatePay(payDTO);
    }

    /**
     * 按照主键记录查询支付流水信息
     * @param id
     * @return
     */
    @GetMapping(value = "/feign/pay/get/{id}")
    public ResultData getPayInfo(@PathVariable("id") Integer id) {

        System.out.println("-------支付微服务远程调用，按照id查询订单支付流水信息");
        ResultData payInfo = null;
        try {
            System.out.println("调用开始--------: " + DateUtil.now());
            payInfo = payFeignApi.getPayInfo(id);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("调用结束--------: " + DateUtil.now());
            payInfo = ResultData.fail(ReturnCodeEnum.RC500.getCode(), e.getMessage());
        }

        return payInfo;
    }

    /**
     * 查询所有流水记录
     * @return
     */
    @GetMapping("/feign/get")
    public ResultData getAll() {
        return payFeignApi.getAll();
    }


    /**
     * openfeign天然支持负载均衡
     * @return
     */
    @GetMapping(value = "/feign/pay/mylb")
    public String mylb() {

        return payFeignApi.mylb();
    }


}






































